#!/bin/bash

echo "１.清理环境"
mkdir -p config
mkdir -p crypto-config
rm -rf ./config/*
rm -rf ./crypto-config/*

echo "2.生成组织基础结构"
cryptogen generate --config=./crypto-config.yaml
echo ""

echo "３.生成初始区块文件"
configtxgen -profile TwoOrgsOrdererGenesis  -outputBlock ./config/genesis.block
echo ""

echo "4.生成通道交易配置文件"
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./config/channel.tx -channelID mychannel
echo ""

echo "5.生成锚节点更新文件"
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./config/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./config/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./config/Org3MSPanchors.tx -channelID mychannel -asOrg Org3MSP
echo ""

echo "6.启动网络节点"
docker-compose -f docker-compose-cli.yaml up -d
echo "Wait 10 seconds"
sleep 10
echo ""

echo "7.创建通道"
docker exec cli peer channel create -o orderer.gdzce.cn:7050 -c mychannel -f ./config/channel.tx
echo ""

echo "8.将各个节点加入通道"
docker exec cli peer channel join -b mychannel.block
docker exec  -e "CORE_PEER_LOCALMSPID=Org1MSP"  -e "CORE_PEER_ADDRESS=node1.organization1.gdzce.cn:7051"  -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization1.gdzce.cn/users/Admin@organization1.gdzce.cn/msp" cli peer channel join -b mychannel.block
docker exec  -e "CORE_PEER_LOCALMSPID=Org2MSP"  -e "CORE_PEER_ADDRESS=node1.organization2.gdzce.cn:7051"  -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.gdzce.cn/users/Admin@organization2.gdzce.cn/msp" cli peer channel join -b mychannel.block
docker exec  -e "CORE_PEER_LOCALMSPID=Org2MSP"  -e "CORE_PEER_ADDRESS=node2.organization2.gdzce.cn:7051"  -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.gdzce.cn/users/Admin@organization2.gdzce.cn/msp" cli peer channel join -b mychannel.block
docker exec  -e "CORE_PEER_LOCALMSPID=Org3MSP"  -e "CORE_PEER_ADDRESS=node1.organization3.gdzce.cn:7051"  -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.gdzce.cn/users/Admin@organization3.gdzce.cn/msp" cli peer channel join -b mychannel.block
docker exec  -e "CORE_PEER_LOCALMSPID=Org3MSP"  -e "CORE_PEER_ADDRESS=node2.organization3.gdzce.cn:7051"  -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.gdzce.cn/users/Admin@organization3.gdzce.cn/msp" cli peer channel join -b mychannel.block
echo ""

echo "９.在背书节点安装链码"
docker exec cli peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/agricultural-products
docker exec  -e "CORE_PEER_LOCALMSPID=Org2MSP"  -e "CORE_PEER_ADDRESS=node2.organization2.gdzce.cn:7051"  -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.gdzce.cn/users/Admin@organization2.gdzce.cn/msp" cli peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/agricultural-products
docker exec  -e "CORE_PEER_LOCALMSPID=Org3MSP"  -e "CORE_PEER_ADDRESS=node2.organization3.gdzce.cn:7051"  -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.gdzce.cn/users/Admin@organization3.gdzce.cn/msp" cli peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/agricultural-products
echo ""

echo "10.链码实例化及交互"
docker exec cli peer chaincode instantiate -o orderer.gdzce.cn:7050 -C mychannel -n mycc -l golang -v 1.0 -c '{"Args":["init"]}' -P "AND ('Org1MSP.member','Org2MSP.member','Org3MSP.member')"
echo "Wait 10 seconds"
sleep 10
echo ""

docker exec  -e "CORE_PEER_LOCALMSPID=Org2MSP"  -e "CORE_PEER_ADDRESS=node2.organization2.gdzce.cn:7051"  -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.gdzce.cn/users/Admin@organization2.gdzce.cn/msp" cli peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
docker exec  -e "CORE_PEER_LOCALMSPID=Org3MSP"  -e "CORE_PEER_ADDRESS=node2.organization3.gdzce.cn:7051"  -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.gdzce.cn/users/Admin@organization3.gdzce.cn/msp" cli peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'

#peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
